﻿using System.Threading;
using System.Threading.Tasks;

namespace ESRI.ArcGIS.Client.Tasks.Async
{
    public static partial class FindTaskExtensions
    {
        #region Execute

        /// <summary>
        /// Executes an Find against an ArcGIS Server map layer.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="parameters">Specifies the layers and fields that are used to search against.</param>
        /// <returns></returns>
        public static Task<FindTaskResult> ExecuteTaskAsync(this FindTask task, FindParameters parameters)
        {
            return ExecuteTaskAsync(task, parameters, CancellationToken.None);
        }

        /// <summary>
        /// Executes an Find against an ArcGIS Server map layer.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="parameters">Specifies the layers and fields that are used to search against.</param>
        /// <param name="cancellationToken">Cancellation token for cancelling task.</param>
        /// <returns></returns>
        public static Task<FindTaskResult> ExecuteTaskAsync(this FindTask task, FindParameters parameters,
            CancellationToken cancellationToken)
        {
            return task.HandleTask<FindTask, FindEventArgs, FindTaskResult>(
                t => t.ExecuteAsync(parameters, null),
                (t, e) => t.ExecuteCompleted += e,
                (t, e) => t.ExecuteCompleted -= e,
                e => new FindTaskResult(e),
                cancellationToken);
        }

        #endregion
    }
}
